

//
//  XiaoleWeb.swift
//  xiaoleservice
//
//  Created by Wenzhe Fan on 2019/6/1.
//  Copyright © 2019年 Wenzhe Fan. All rights reserved.
//

import UIKit
import WebKit

class XiaoleWeb: UIViewController {
    var url:String?
    @IBOutlet weak var webView: WKWebView!
    @IBOutlet weak var 选择: UIButton!
    var type:LargeType = .信源
    override func viewDidLoad() {
        super.viewDidLoad()
        self.clearCache()
        self.选择.tag = LargeType.选择.rawValue//url ??
        let myUrlString:String = url ?? "https://www.baidu.com"

        let myURL:URL = URL(string: myUrlString)!
        let myRequest:URLRequest = URLRequest(url: myURL)
        webView.load(myRequest)
    }
    override func loadView() {
        //创建网页加载的偏好设置
        let prefrences = WKPreferences()
        prefrences.javaScriptEnabled = false
        
        //配置网页视图
        let webConfiguration = WKWebViewConfiguration()
        webConfiguration.preferences = prefrences
        webConfiguration.preferences.minimumFontSize = 9.0
        self.webView = WKWebView(frame: .zero, configuration: webConfiguration)
        self.webView.navigationDelegate = self as? WKNavigationDelegate;
        view = self.webView
    }
    func clearCache() -> Void {
        URLCache.shared.removeAllCachedResponses();
        URLCache.shared.diskCapacity = 0;
        URLCache.shared.memoryCapacity = 0;
        
    }
    @IBAction func cancelHander(_ sender: UIButton) {
        
    }
    @objc func p() {
        
    }
    
    @IBAction func touchDwonHande(_ sender: UIButton) {
        type = LargeType(rawValue: sender.tag)!
        switch type {
        case .信源:
            break
        case .服务:
            break
        case .建议:
            break
        case .选择:
            self.dismiss(animated: true, completion: nil)
            break
        }
    }
    
    @IBAction func touchDwonRepeat(_ sender: UIButton) {
       
    }
    
    
    @IBAction func touchDragEnter(_ sender: UIButton) {
        
    }
    
    @IBAction func touchDragExit(_ sender: UIButton) {
        
    }
    
    
    @IBAction func touchDragInSider(_ sender: UIButton) {
        
    }
    
    @IBAction func touchDragOutSider(_ sender: UIButton) {
        
    }
    
    
    @IBAction func touchupInSider(_ sender: UIButton) {
        
    }
    
    
    @IBAction func touchdownOutSider(_ sender: UIButton) {
        
    }
}
extension ViewController: WKNavigationDelegate {
    //视图开始载入的时候显示网络活动指示器
    func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = true
    }
    
    //载入结束后，关闭网络活动指示器
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        UIApplication.shared.isNetworkActivityIndicatorVisible = false
    }
    
    //阻止链接被点击
    func webView(_ webView: WKWebView, decidePolicyFor navigationAction: WKNavigationAction, decisionHandler: @escaping (WKNavigationActionPolicy) -> Void) {
        if navigationAction.navigationType == .linkActivated {
            decisionHandler(.cancel)
            
            let alertController = UIAlertController(title: "Action not allowed", message: "Tapping on links is not allowed. Sorry!", preferredStyle: .alert)
            alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: nil))
            present(alertController, animated: true, completion: nil)
            return
            
        }
        decisionHandler(.allow)
    }
    func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
        webView.reload()
    }
    func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
        webView.reload()
    }
    func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
        // 判断服务器采用的验证方法
        if challenge.protectionSpace.authenticationMethod == NSURLAuthenticationMethodServerTrust {
            if challenge.previousFailureCount == 0 {
                // 如果没有错误的情况下 创建一个凭证，并使用证书
                let credential = URLCredential(trust: challenge.protectionSpace.serverTrust!)
                completionHandler(.useCredential, credential)
            } else {
                // 验证失败，取消本次验证
                completionHandler(.cancelAuthenticationChallenge, nil)
            }
        } else {
            completionHandler(.cancelAuthenticationChallenge, nil)
        }
    }

}
